﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using roster.Domain;

namespace roster.UI
{
    public partial class RosterView : Form, IRosterView
    {
        private string m_OpenedFile = string.Empty;

        public RosterView()
        {
            InitializeComponent();
            Closing += RosterView_Closing;
        }

        #region IRosterView Members

        public event MethodInvoker AddPlayer;
        public event MethodInvoker Randomize;
        public event MethodInvoker CreateRoster;
        public event Action<IPlayer> Remove;
        public event Action<IPlayer> MoveUp;
        public event Action<IPlayer> MoveDown;
        public event Action<IEnumerable<IPlayer>> CreateRaid;
        public event Action<string> CreateRaidFromFile;
        public event Action<string> LoadRoster;
        public event Action<string> SaveRoster;

        public string Title
        {
            get { return Text; }
            set { Text = value; }
        }

        public void UpdateView(IEnumerable<IPlayer> players)
        {
            lstPlayers.AddPlayers(players);
            Refresh();
        }

        public void HideView()
        {
            Hide();
        }

        public void EnableAction()
        {
            btnAdd.Enabled = true;
            btnRemove.Enabled = true;
            btnMoveDown.Enabled = true;
            btnMoveUp.Enabled = true;
            btnCreateRaidSelection.Enabled = true;
            btnCreateRaidFile.Enabled = true;
        }

        public void SelectPlayer(IPlayer player)
        {
            lstPlayers.SelectedPlayer = player;
        }

        #endregion

        private void RosterView_Closing(object sender, CancelEventArgs e)
        {
        	// MK 7/8/08 - We should only ask the user to save changes if there actually are changes
        	//  to be saved - how do we get at that information?
        	
            DialogResult result = MessageBox.Show("Save changes to roster?", "Save Roster?", MessageBoxButtons.YesNoCancel,
                                                  MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

            if (result == DialogResult.Yes)
            {
                OpenSaveRosterDialog();
            }
            else if (result == DialogResult.Cancel)
            {
            	e.Cancel = true;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddPlayer();
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            ExecuteOnSelectedPlayer(MoveUp);
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            ExecuteOnSelectedPlayer(MoveDown);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            ExecuteOnSelectedPlayer(Remove);
        }

        private void ExecuteOnSelectedPlayer(Action<IPlayer> call)
        {
            var player = lstPlayers.SelectedPlayer;
            if (player != null)
            {
                call(player);
            }
        }

        private void btnCreateRaidSelection_Click(object sender, EventArgs e)
        {
            if (lstPlayers.HasSelectedPlayers)
            {
                CreateRaid(lstPlayers.SelectedPlayers);
            }
        }
        
        private void btnCreateRaidFile_Click(object sender, EventArgs e)
        {
            DialogResult result = m_openRaidFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
            	CreateRaidFromFile(m_openRaidFileDialog.FileName);
            }
        }        

        private void loadRosterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = m_OpenFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                m_OpenedFile = m_OpenFileDialog.FileName;
                LoadRoster(m_OpenFileDialog.FileName);
            }
        }

        private void saveRosterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenSaveRosterDialog();
        }

        private void OpenSaveRosterDialog()
        {
            m_SaveFileDialog.FileName = m_OpenedFile;
            DialogResult result = m_SaveFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                SaveRoster(m_SaveFileDialog.FileName);
            }
        }

        private void randomizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult randomizeVerification = MessageBox.Show("Are you sure you want to randomize the list?",
                                                                 "Randomize Warning", MessageBoxButtons.OKCancel,
                                                                 MessageBoxIcon.Exclamation,
                                                                 MessageBoxDefaultButton.Button2);

            if (randomizeVerification == DialogResult.OK)
            {
                Randomize();
            }
        }

        private void createRosterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateRoster();
        }
    }
}